'use strict'

//创建文件流,使default内容与index内容相等
let fs = require('fs');

let x1 = fs.createReadStream('./index.html');
let x2 = fs.createWriteStream('./default.html');

x1.pipe(x2);

let port = 7447;
let http = require('http');
// const { stat } = require('node:fs');
let path = require('path');

http.createServer((req, res) => {
    //a.获取请求的信息
    let reqPath = req.url; //根目录
    // //b.根据信息处理业务或逻辑
    let currentPath = __dirname;//当前项目所在目录
    console.log(currentPath);
    let fullPath = path.join(currentPath, reqPath);
    console.log(fullPath);
    fs.readdir(fullPath, (err, files) => {
        if (err) {
            return console.log(err);
        }
        files.forEach((file) => {
            console.log(file);
        })
    })
    fs.readFile(fullPath, (err, data) => {
        if (err) {
            res.writeHead(404)
            res.end('404 not found')
        } else {
            res.end(data)
        }
    })
    fs.stat('index.html',(err,stat)=>{
        if(err){
            console.log(err);
        }else{
            console.log('IsFile:'+stat.isFile());
            console.log('IsDirectory:'+stat.isDirectory());
            if(stat.isFile()){
                console.log('size:'+stat.size);
                console.log('birth time:'+stat.birthtime);
                console.log('modified time:'+stat.mtime);
            }
        }
    })
}).listen(port)

console.log(`Server is running at Http://127.0.0.1:${port}`);